package com.frxuexi.park2209.sign

import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MapView
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.mvvm.BaseActivity
import com.frxuexi.base.net.Const
import com.frxuexi.park2209.databinding.ActivitySignBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import kotlin.concurrent.timerTask


@AndroidEntryPoint
class SignActivity : BaseActivity<ActivitySignBinding,SignViewModel>() {

    var mMapView: MapView? = null
    var mBaiduMap:BaiduMap?=null
    lateinit var timer:Timer

    override fun initData() {
        lifecycleScope.launch {
            vm.signSuc.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong(it)
                }
            }
        }
    }

    override fun initView() {
        requestPermissions(arrayOf("android.permission.ACCESS_COARSE_LOCATION","android.permission.ACCESS_FINE_LOCATION",
            "android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.ACCESS_NETWORK_STATE","android.permission.ACCESS_WIFI_STATE"),100)
        initMap()
        binding.curTime.text=getTime(System.currentTimeMillis())
        lifecycleScope.launch(Dispatchers.IO) {
            timer= Timer()
            timer.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main){
                    binding.curTime.text=getTime(System.currentTimeMillis())
                }
            },0,1000)
        }

        if(SPUtils.getInstance().getBoolean("sign",false)) {
            //已经签到过
            if(SPUtils.getInstance().getBoolean("sout",false)){
                binding.btnSign.isEnabled=false
            }else{
                binding.btnSign.text="签退"
            }
        }


        binding.btnSign.setOnClickListener {
            if(SPUtils.getInstance().getBoolean("sign",false)) {
                //已经签到过
                SPUtils.getInstance().put("sout",true)
                binding.btnSign.isEnabled=false
            }else{
                SPUtils.getInstance().put("sign",true)
                binding.btnSign.text="签退"
            }
            val map = mutableMapOf<String, Any>()
            map["endAddress"] = "天津市"
            map["endTime"] = getDataTimeT(System.currentTimeMillis())
            map["startAddress"] = "天津市"
            map["startTime"] = getDataTimeT(System.currentTimeMillis())
            map["userId"] = SPUtils.getInstance().getInt(Const.UID)
            vm.sign(map)
        }
    }

    fun initMap(){
        //获取地图控件引用
        mMapView = binding.bmapView;
        mBaiduMap= mMapView!!.map
        mBaiduMap!!.setMyLocationEnabled(true);

        LocationClient.setAgreePrivacy(true)




//可选，是否需要位置描述信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的位置信息，此处必须为true



        //定位初始化
        //定位初始化
        var mLocationClient = LocationClient(this)

//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        val option = LocationClientOption()
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)
        option.setIsNeedLocationDescribe(true);
        option.setIsNeedAddress(true);
//可选，是否需要地址信息，默认为不需要，即参数为false
//如果开发者需要获得当前点的地址信息，此处必须为true

        option.setNeedNewVersionRgc(true);
//设置locationClientOption

//设置locationClientOption
        mLocationClient.setLocOption(option)

//注册LocationListener监听器

//注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient.registerLocationListener(myLocationListener)
//开启地图定位图层
//开启地图定位图层
        mLocationClient.start()
    }


    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            //定位蓝点额外加的
            var ll = LatLng(location.latitude, location.longitude);

            val locationDescribe = location.locationDescribe //获取位置描述信息

            val addr = location.addrStr //获取详细地址信息

            if(!addr.isNullOrEmpty()) {
                lifecycleScope.launch(Dispatchers.Main) {
                    binding.tvLoc.text = addr
                }
            }

            val update = MapStatusUpdateFactory.newLatLngZoom(ll, 5f);
            Log.i("===","${locationDescribe},"+locationDescribe)



            mBaiduMap!!.animateMapStatus(update);
            mBaiduMap!!.setMyLocationData(locData)
        }
    }

}